package com.zhaoc.service.impl;

import com.zhaoc.dao.AreaRepository;
import com.zhaoc.dao.FixAreaRepository;
import com.zhaoc.dao.OrderRepository;
import com.zhaoc.domain.Area;
import com.zhaoc.domain.Courier;
import com.zhaoc.domain.FixedArea;
import com.zhaoc.domain.SubArea;
import com.zhaoc.domain.constans.Constants;
import com.zhaoc.domain.take_delivery.Order;
import com.zhaoc.service.IOrderService;
import org.apache.cxf.jaxrs.client.WebClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.ws.rs.core.MediaType;
import java.util.Date;
import java.util.Iterator;
import java.util.UUID;

/**
 * <p>title:com.zhaoc.service.impl</p>
 * date:2018/8/14 0014
 * time:21:11
 * author:赵成
 * user:13566
 */
@Service
@Transactional
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private FixAreaRepository fixAreaRepository;

    @Autowired
    private OrderRepository orderRepository;

    @Autowired
    private AreaRepository areaRepository;

    @Override
    public void saveOrder(Order order) {

        //设置订单号
        order.setOrderNum(UUID.randomUUID().toString());
        //待取件
        order.setStatus("1");
        //设置下单时间
        order.setOrderTime(new Date());

        //根据订单获取发件人和收件人地址的区域
        Area sendArea = order.getSendArea();
        Area persistSendArea = areaRepository.findByProvinceAndCityAndDistrict
                (sendArea.getProvince(), sendArea.getCity(),
                sendArea.getDistrict());

        Area recArea = order.getRecArea();
        Area persistRecArea = areaRepository.findByProvinceAndCityAndDistrict(recArea
                        .getProvince(), recArea.getCity(),
                recArea.getDistrict());

        order.setSendArea(persistSendArea);
        order.setRecArea(persistRecArea);


        //自动分单逻辑,首先判断客户表中是否有和下单地址相同的地址,如果有,可以直接通过数据库获取对应的定区ID
        String fixedAreaId = WebClient.create(Constants.CRM_MANAGEMENT_HOST +
                "/crm_management/services/customerService" +
                "/customer/findFixedAreaIdByAddress").accept(MediaType.APPLICATION_JSON).get
                (String.class);
        if (fixedAreaId != null) {
            //找到了对应的定区ID,直接分配给对应的快递员
            FixedArea fixedArea = fixAreaRepository.findOne(fixedAreaId);
            Courier courier = fixedArea.getCouriers().iterator().next();
            if (saveOrder(order, courier)) return;
        }

        //根据详细地址自动分单失败
        //根据分区关键字自动匹配分单
        for (SubArea subArea : persistSendArea.getSubareas()) {
            //判断当前客户的下单地址是否包含分区关键字
            if (order.getSendAddress().contains(subArea.getKeyWords())) {
                //包含分区关键字,找到分区,找到定区,找到快递员
                Iterator<Courier> iterator = subArea.getFixedArea().getCouriers().iterator();
                if (iterator.hasNext()){
                    Courier courier = iterator.next();
                    if (saveOrder(order, courier)) return;
                }
            }
        }

        //根据分区关键字自动分单失败
        //根据辅助关键字查找
        for (SubArea subArea : persistSendArea.getSubareas()) {
            if (order.getSendAddress().contains(subArea.getAssistKeyWords())) {
                Courier courier = subArea.getFixedArea().getCouriers().iterator().next();
                if (saveOrder(order, courier)) return;
            }
        }

        //进入人工分单
        order.setOrderType("2");
        orderRepository.save(order);
    }

    //根据订单编号,查询订单
    @Override
    public Order findByOrderNum(String orderNum) {
        return orderRepository.findByOrderNum(orderNum);
    }

    private boolean saveOrder(Order order, Courier courier) {
        if (courier != null) {
            //自动派单成功
            System.out.println("自动分单成功...");
            //将快递员关联订单
            order.setCourier(courier);
            //保存订单

            orderRepository.save(order);
            return true;
        }
        return false;
    }
}
